package cn.deepxi.org.controller;

import cn.deepxi.org.domain.Department;
import cn.deepxi.org.domain.Employee;
import cn.deepxi.org.query.EmpQuery;
import cn.deepxi.org.service.IEmpService;
import cn.deepxi.utils.AjaxResult;
import cn.deepxi.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工的API",description="员工相关的CRUD功能")
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private IEmpService employeeService;
    @ApiOperation(value = "新增修改的接口" )
    @PutMapping
    public AjaxResult addOrUpt(@ApiParam(name="传递对象",required = true) @RequestBody Employee employee){
        try{
            if(employee.getId()==null){
                employeeService.add(employee);
            }else {
                employeeService.upt(employee);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.erro();
    }
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.erro();
        }
    }
    @PostMapping
    public QueryResult<Employee> querryPage(@RequestBody EmpQuery empQuery){
        return employeeService.queryData(empQuery);
    }
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
            return employeeService.findById(id);
    }
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.erro();
    }
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

}
